home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 2010 April
/
PCWorld0410.iso
/
pluginy Firefox
/
11255
/
11255.xpi
/
chrome
/
content
/
controller
/
Toolbar.js
< prev
next >
Wrap
Text File
|
2010-02-10
|
47KB
|
1,261 lines
/* ***** BEGIN LICENSE BLOCK *****
*
* Pearltrees add-on AMO, Copyright(C), 2009, Broceliand SAS, Paris, France
* (company in charge of developing Pearltrees)
*
* This file is part of ΓÇ£Pearltrees add-on AMOΓÇ¥.
*
* Pearltrees add-on AMO is free software: you can redistribute it and/or modify it under the
* terms of the GNU General Public License version 3 as published by the Free Software Foundation.
*
* Pearltrees add-on AMO is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with Pearltrees add-on AMO.
* If not, see <http://www.gnu.org/licenses/>
*
* ***** END LICENSE BLOCK ***** */
/////////////////////////////////////////////////////////////////////////////////
// Global actions
/////////////////////////////////////////////////////////////////////////////////
const RECORING_MODE_ONE_BY_ONE = 1;
const RECORING_MODE_CONTINUOUS = 2;
const DEFAULT_RECORDING_MODE = RECORING_MODE_ONE_BY_ONE;
const SYNC_TREE_SELECTION_WITH_FLEX = false;
const PERSIST_RECORDING_MODE = false;
const URL_RECORDED_BEFORE_ALERT = 25;
const ALERT_STEP_1 = 25;
const ALERT_STEP_2 = 50;
const ALERT_STEP_3 = 75;
const MAX_PEARLS_IN_TREE = 100;
/**
* Global variables like recording state or visibility.
*/
var BRO_toolbar = {
isOnline:false,
isRecording:false,
isHidden:true,
isRevealed:true,
isFirstInstallMode:false,
isUpdate:false,
isUserLogged:false,
recordingMode: DEFAULT_RECORDING_MODE,
lastUrlRecorded:null,
isInit:false,
showHelpOnRecording:false,
helpStartupWindow:null,
nameMapWindow:null,
manyPearlsWindow:null,
optionWindow:null,
initTime:null,
viewLoaded:false,
init: function() {
BRO_toolbar.isInit = true;
// Log
BRO_log.init();
var d = new Date();
BRO_toolbar.initTime = d.getTime();
BRO_log.log("initializing");
// Local
BRO_locale.init();
BRO_toolbar.validateEnv();
// Model
BRO_model.init();
BRO_windowManager.init();
// Listeners
BRO_browserManager.init();
BRO_actionListener.init();
BRO_listenerHandler.init();
// Download listener is disabled
//BRO_downloadListener.init();
BRO_extensionManagerListener.init();
BRO_toolbar.initFlexCommandListener();
window.addEventListener("online", BRO_toolbar.onOnline, false);
window.addEventListener("offline", BRO_toolbar.onOffline, false);
// Detect installation / updates and load user stats
BRO_toolbar.loadPreferences();
// UI
BRO_ButtonsHandler.init(this.recordingMode);
},
onUninstall:function() {
BRO_log.log("Uninstalling");
BRO_toolbar.resetPreferences();
BRO_ButtonsHandler.removeAllButtons();
},
onFirstInstall:function() {
BRO_log.log("Installing");
BRO_toolbar.isFirstInstall = true;
BRO_model.clear();
if(BRO_toolbar.viewLoaded) {
BRO_ButtonsHandler.addFirstInstallButtonInNavBar();
BRO_model.getTreesAndCurrentUser(skipNotificationIfNotLogged = true);
}
},
onUpdate:function() {
BRO_log.log("Updating");
BRO_toolbar.isUpdate = true;
BRO_model.clear();
if(BRO_toolbar.viewLoaded && BRO_ButtonsHandler.restoreDefaultPostionOnLoad) {
BRO_ButtonsHandler.restoreDefaultPositionInNavbar();
BRO_ButtonsHandler.restoreDefaultPostionOnLoad = false;
BRO_model.getTreesAndCurrentUser(skipNotificationIfNotLogged = true);
}
},
onDownloadNotifiedToAMO:function() {
var prefs = Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefService)
.getBranch("extensions.bro_toolbar.");
prefs.setBoolPref("amo_dl_notified", true);
BRO_log.log("AMO notified of download");
},
onActiveUserNotifiedToAMO:function() {
var prefs = Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefService)
.getBranch("extensions.bro_toolbar.");
prefs.setCharPref("amo_active_date", BRO_toolbar.formatCurrentDayDate());
BRO_log.log("AMO notified of being active");
},
formatCurrentDayDate:function() {
var date = new Date();
return date.getFullYear()+""+(date.getMonth()+1)+""+date.getDate();
},
isThirdPartyCookiesEnabled:function() {
var prefs = Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefService)
.getBranch("network.cookie.");
var cookieBehavior = prefs.getIntPref("cookieBehavior");
return (cookieBehavior == 0)?true:false;
},
enableThirdPartyCookies:function() {
var prefs = Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefService)
.getBranch("network.cookie.");
prefs.setIntPref("cookieBehavior", 0);
},
/**
* @todo create a custom dialog in order to remove the default image:
* @see https://developer.mozilla.org/en/Code_snippets/Dialogs_and_Prompts
*
* @return boolean
*/
isUserWantToEnableThirdPartyCookies:function() {
var prompts = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
.getService(Components.interfaces.nsIPromptService);
return prompts.confirm(window, "", BRO_locale.getString('cookiesError.text'));
},
resetPreferences:function() {
var prefs = Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefService)
.getBranch("extensions.bro_toolbar.");
prefs.setBoolPref("firstrun", true);
prefs.setBoolPref("firstuse", true);
prefs.setBoolPref("isFirstInstallMode", true);
prefs.setBoolPref("amo_dl_notified", false);
prefs.setCharPref("amo_active_date", "");
prefs.setCharPref("version", "");
prefs.setCharPref("currentUser", "");
prefs.setCharPref("rootTree", "");
prefs.setCharPref("dropZone", "");
prefs.setIntPref("recordingMode", DEFAULT_RECORDING_MODE);
},
loadPreferences:function() {
var prefs = Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefService)
.getBranch("extensions.bro_toolbar.");
var ver = -1;
var firstrun = true;
var firstuse = true;
var isFirstInstallMode = true;
var amoDownloadNotified = false;
var amoLastActiveDate = "";
var currentUser = null;
this.showHelpOnRecording = false;
var gExtensionManager = Components.classes["@mozilla.org/extensions/manager;1"]
.getService(Components.interfaces.nsIExtensionManager);
var current = gExtensionManager.getItemForID(BRO_ADDON_ID).version;
try {
ver = prefs.getCharPref("version");
firstrun = prefs.getBoolPref("firstrun");
firstuse = prefs.getBoolPref("firstuse");
isFirstInstallMode = prefs.getBoolPref("isFirstInstallMode");
amoDownloadNotified = prefs.getBoolPref("amo_dl_notified");
amoLastActiveDate = prefs.getCharPref("amo_active_date");
}catch(e){
//nothing
}finally {
// if it is the first pearlbar installation
if (firstrun) {
prefs.setBoolPref("firstrun", false);
firstuse = true;
prefs.setBoolPref("firstuse", firstuse);
isFirstInstallMode = true;
prefs.setBoolPref("isFirstInstallMode", true);
prefs.setCharPref("version",current);
BRO_toolbar.onFirstInstall();
}
// if this is a pearlbar update
else if (ver!=current) {
var versionChecker = Components.classes["@mozilla.org/xpcom/version-comparator;1"]
.getService(Components.interfaces.nsIVersionComparator);
if(versionChecker.compare(ver, "5.0") < 0) {
BRO_ButtonsHandler.restoreDefaultPostionOnLoad = true;
firstuse = true;
prefs.setBoolPref("firstuse", firstuse);
}
prefs.setCharPref("version", current);
BRO_toolbar.onUpdate();
this.showHelpOnRecording = true;
}
this.isFirstInstallMode = isFirstInstallMode;
this.showHelpOnRecording = firstuse;
if(PERSIST_RECORDING_MODE) {
this.recordingMode = this.backupRecordingModeFromPreferences();
}
if(BRO_ADDON_SOURCE == BRO_ADDON_SOURCE_SELFHOSTED) {
if(!amoDownloadNotified) {
BRO_model.notifyDownloadToAMO();
}
if(amoLastActiveDate != BRO_toolbar.formatCurrentDayDate()) {
BRO_model.notifyActiveUserToAMO();
}
}
BRO_model.updateCurrentUser(this.backupCurrentUserFromPreferences());
}
},
backupCurrentUserFromPreferences:function() {
var prefs = Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefService)
.getBranch("extensions.bro_toolbar.");
var currentUser = null;
try{
currentUser = prefs.getCharPref("currentUser");
}catch(e){}
return (currentUser && currentUser != '')?BRO_model._json.decode(currentUser):null;
},
saveCurrentUserIntoPreferences:function(currentUser) {
var prefs = Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefService)
.getBranch("extensions.bro_toolbar.");
var encodedUser = (currentUser)?BRO_model._json.encode(currentUser):'';
prefs.setCharPref("currentUser", encodedUser);
},
backupRootTreeFromPreferences:function() {
var prefs = Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefService)
.getBranch("extensions.bro_toolbar.");
var rootTree = null;
try{
rootTree = prefs.getCharPref("rootTree");
}catch(e){}
return (rootTree && rootTree != '')?BRO_model._json.decode(rootTree):null;
},
saveRootTreeIntoPreferences:function(rootTree) {
var prefs = Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefService)
.getBranch("extensions.bro_toolbar.");
var encodedRootTree = (rootTree)?BRO_model._json.encode(rootTree):'';
prefs.setCharPref("rootTree", encodedRootTree);
},
backupDropZoneFromPreferences:function() {
var prefs = Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefService)
.getBranch("extensions.bro_toolbar.");
var dropZone = null;
try{
dropZone = prefs.getCharPref("dropZone");
}catch(e){}
return (dropZone && dropZone != '')?BRO_model._json.decode(dropZone):null;
},
saveDropZoneIntoPreferences:function(dropZone) {
var prefs = Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefService)
.getBranch("extensions.bro_toolbar.");
var encodedDropZone = (dropZone)?BRO_model._json.encode(dropZone):'';
prefs.setCharPref("dropZone", encodedDropZone);
},
backupRecordingModeFromPreferences:function() {
var prefs = Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefService)
.getBranch("extensions.bro_toolbar.");
var recordingMode = DEFAULT_RECORDING_MODE;
try{
recordingMode = prefs.getIntPref("recordingMode");
}catch(e){
//nothing
}
finally{
if(recordingMode != RECORING_MODE_ONE_BY_ONE && recordingMode != RECORING_MODE_CONTINUOUS) {
recordingMode = DEFAULT_RECORDING_MODE;
}
return recordingMode;
}
},
saveRecordingModeIntoPreferences:function(recordingMode) {
if(recordingMode != RECORING_MODE_ONE_BY_ONE && recordingMode != RECORING_MODE_CONTINUOUS) {
return;
}
var prefs = Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefService)
.getBranch("extensions.bro_toolbar.");
prefs.setIntPref("recordingMode", recordingMode);
},
setFirstUse:function(value) {
var prefs = Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefService)
.getBranch("extensions.bro_toolbar.");
prefs.setBoolPref("firstuse",value);
this.showHelpOnRecording = value;
},
setFirstInstallMode:function(value) {
if(this.isFirstInstallMode && !value) {
BRO_ButtonsHandler.restoreDefaultPositionInNavbar();
// There is a bug while calculating the button width at this time
// BRO_ButtonsHandler.onButtonsCreated();
BRO_recordButtonController.refreshModeSelection();
BRO_inButtonController.refreshTreeListVisualComponents();
}
this.isFirstInstallMode = value;
var prefs = Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefService)
.getBranch("extensions.bro_toolbar.");
prefs.setBoolPref("isFirstInstallMode", value);
},
onQuit:function() {
BRO_model.clear();
},
addUrlRecorded:function(url) {
BRO_toolbar.lastUrlRecorded = url;
BRO_noteController.init();
BRO_toolbar.addPearlCountToSelection();
BRO_toolbar.addUrlRecordedCountToSelection();
var pearlCount = this.getSelectionPearlCount();
var urlRecordedCount = this.getSelectionUrlRecordedCount();
var isDropZoneSelected = BRO_inButtonController.isDropZoneSelected();
if((pearlCount == ALERT_STEP_1 || pearlCount == ALERT_STEP_2 || pearlCount == ALERT_STEP_3)
&& urlRecordedCount >= URL_RECORDED_BEFORE_ALERT
&& !isDropZoneSelected
&& this.recordingMode == RECORING_MODE_CONTINUOUS) {
BRO_toolbar.setRecordingMode(RECORING_MODE_ONE_BY_ONE);
BRO_log.log("Alert. Current tree has "+pearlCount+ " pearls");
BRO_tools.callWithDelay('BRO_toolbar.showManyPearlsWindow()', BRO_buttonEffectHelper.START_RECORDING_EFFECT_TIME);
}
},
isCurrentTreeFull:function() {
var pearlCount = this.getSelectionPearlCount();
var isDropZoneSelected = BRO_inButtonController.isDropZoneSelected();
if(pearlCount >= MAX_PEARLS_IN_TREE && !isDropZoneSelected) {
BRO_toolbar.showTreeFullWindow();
return true;
}
return false;
},
getSelectedItem:function() {
if(BRO_inButtonController.getSelectedTree()) {
return BRO_inButtonController.getSelectedTree();
}
else if(BRO_inButtonController.getSelectedHistory()){
return BRO_inButtonController.getSelectedHistory();
}
else if(BRO_inButtonController.getSelectedNewHistory()) {
return BRO_inButtonController.getSelectedNewHistory();
}
else{
return null;
}
},
addPearlCountToSelection:function() {
if(!this.getSelectedItem()) return;
var pearlCount = this.getSelectionPearlCount();
this.getSelectedItem().pearlCount = pearlCount + 1;
},
addUrlRecordedCountToSelection:function() {
if(!this.getSelectedItem()) return;
var urlRecordedCount = this.getSelectionUrlRecordedCount();
this.getSelectedItem().urlRecordedCount = urlRecordedCount + 1;
},
getSelectionPearlCount:function() {
var selectedItem = this.getSelectedItem();
if(!selectedItem) return 0;
var pearlCount = parseInt(selectedItem.pearlCount);
if(isNaN(pearlCount)) {
return 0;
}else{
return pearlCount;
}
},
getSelectionUrlRecordedCount:function() {
var selectedItem = this.getSelectedItem();
if(!selectedItem) return 0;
var urlRecordedCount = parseInt(selectedItem.urlRecordedCount);
if(isNaN(urlRecordedCount)) {
return 0;
}else{
return urlRecordedCount;
}
},
isCurrentUrlRecorded:function() {
return (this.lastUrlRecorded == BRO_browserManager.getSelectedBrowserUrl());
},
getRecordingMode:function() {
return this.recordingMode;
},
setRecordingMode:function(value) {
if(this.recordingMode != RECORING_MODE_ONE_BY_ONE && this.recordingMode != RECORING_MODE_CONTINUOUS) {
return;
}
if(this.recordingMode == value) {
return;
}
this.recordingMode = value;
if(PERSIST_RECORDING_MODE) {
this.saveRecordingModeIntoPreferences(this.recordingMode);
}
// oneByOne mode
if(this.recordingMode == RECORING_MODE_ONE_BY_ONE) {
// stop recording
if(this.isRecording) {
this.setRecording(false);
}
this.lastUrlRecorded = null;
}
// continuous mode
if(this.recordingMode == RECORING_MODE_CONTINUOUS) {
var currentUser = BRO_model.getCurrentUser();
var selectedTree = BRO_inButtonController.getSelectedTree();
if(selectedTree) {
var selectedTreeId = selectedTree.treeID;
}
if(currentUser) {
var dropZoneID = currentUser.dropZoneID;
}
// start recording
if(!this.isRecording) {
this.setRecording(true);
if(!this.isCurrentUrlRecorded()) {
BRO_ButtonsHandler.recordCurrentPage();
}
}
}
BRO_recordButtonController.refreshModeSelection();
BRO_windowManager.setRecordingMode(value);
},
setRecording:function(value) {
if(this.isRecording == value) return;
this.isRecording = value;
BRO_windowManager.setRecording(value);
if(value) {
// Visually start recording
BRO_ButtonsHandler.startRecording();
// Start browser listeners
BRO_listenerHandler.registerListeners();
// Refresh list if just revealed
if(BRO_toolbar.isRevealed) {
BRO_model.getTreesAndCurrentUser();
}
// A reveal will be needed
BRO_toolbar.isRevealed = false;
if(this.showHelpOnRecording) {
BRO_toolbar.showHelpRecording();
this.setFirstUse(false);
}
}else {
// Reset recording infos
if(this.recordingMode == RECORING_MODE_CONTINUOUS) {
BRO_toolbar.lastUrlRecorded = null;
}
// Remove listeners
BRO_listenerHandler.unregisterListeners();
// Visually stop recording
BRO_ButtonsHandler.stopRecording();
}
},
treeSelectionChanged:function() {
if(this.recordingMode == RECORING_MODE_ONE_BY_ONE) {
if(!BRO_toolbar.isCurrentUrlRecorded()) {
BRO_toolbar.lastUrlRecorded = null;
}
}
},
openCreateAccountPage:function() {
BRO_tools.openURLinCurrentTab(BRO_SERVICE_FF_URL+"createAccount");
},
openLoginPage:function() {
BRO_tools.openURLinCurrentTab(BRO_SERVICE_FF_URL+"login");
},
reveal:function() {
if(this.isRecording) {
BRO_toolbar.setRecording(false);
}
BRO_buttonEffectHelper.runRevealEffect();
BRO_toolbar.lastUrlRecorded = null;
BRO_toolbar.setRecordingMode(RECORING_MODE_ONE_BY_ONE);
var revealDelay = BRO_buttonEffectHelper.REVEAL_EFFECT_TIME + 100;
// If there is a selected tree we reveal into this tree
if(BRO_inButtonController.getSelectedTree()) {
var currentUser = BRO_model.getCurrentUser();
if(currentUser) {
var isOnPearltrees = this.isPearltreesPublicUrl();
var selectedTreeId = BRO_inButtonController.getSelectedTree().treeID;
var flexSelectedTreeID = this.getFlexSelectedTreeIdFromAnchorParams();
if(isOnPearltrees && selectedTreeId == flexSelectedTreeID) {
gBrowser.reload();
}else{
this.openSelectedTreeInCurrentTab(revealDelay);
}
}else{
BRO_tools.openURLinCurrentTab(BRO_PUBLIC_URL+"/#", revealDelay);
}
}
// Else we reveal the current history
else{
BRO_tools.openURLinCurrentTab(BRO_SERVICE_FF_URL+"reveal", revealDelay);
}
BRO_toolbar.isRevealed = true;
},
openSelectedTreeInCurrentTab:function(delay, focusIfSelected) {
var currentUser = BRO_model.getCurrentUser();
var selectedTree = BRO_inButtonController.getSelectedTree();
if(!selectedTree || !currentUser) {
return;
}
var selectedTreeId = selectedTree.treeID;
var isOnPearltrees = this.isPearltreesPublicUrl();
var isDropZoneSelected = (selectedTreeId == currentUser.dropZoneID);
if(isOnPearltrees) {
var flexSelectedTreeID = this.getFlexSelectedTreeIdFromAnchorParams();
var flexFocusedTreeID = this.getFlexFocusedTreeIdFromAnchorParams();
var isAlreadySelected = (selectedTreeId == flexSelectedTreeID);
var isAlreadyFocused = (selectedTreeId == flexFocusedTreeID);
if(isDropZoneSelected || (isAlreadySelected && !focusIfSelected) || isAlreadyFocused) {
return;
}
}
else if(!isOnPearltrees && isDropZoneSelected) {
selectedTreeId = currentUser.rootTreeID;
}
var flexUrl = BRO_toolbar.getCurrentPearltreesPublicUrl();
flexUrl += "#N-u="+currentUser.userDB+"_"+currentUser.userID;
flexUrl += "&N-f="+currentUser.userDB+"_"+selectedTreeId;
flexUrl += "&N-s="+currentUser.userDB+"_"+selectedTreeId;
BRO_log.log('navigate to: '+flexUrl);
BRO_tools.openURLinCurrentTab(flexUrl, delay);
},
onUseToolbar:function() {
BRO_buttonEffectHelper.stopHelpEffects();
},
onLocationChange:function(url) {
// Is on pearltrees
if(this.isPearltreesPublicUrl()) {
var params = this.getCurrentUrlAnchorParams();
// Run & stop samba effect
if(params['Pearlbar-samba']=='1'){
BRO_buttonEffectHelper.runHelpEffects();
}
else {
BRO_buttonEffectHelper.stopHelpEffects();
}
// Sync with Flex navigation
if(SYNC_TREE_SELECTION_WITH_FLEX) {
var flexSelectedUserID = this.getFlexSelectedUserIdFromAnchorParams();
var currentUser = BRO_model.getCurrentUser();
var currentUserID = (currentUser)?currentUser.userID:null;
if(flexSelectedUserID && currentUserID && flexSelectedUserID == currentUserID) {
var flexSelectedTreeID = this.getFlexSelectedTreeIdFromAnchorParams();
var flexFocusedTreeID = this.getFlexFocusedTreeIdFromAnchorParams();
var treeIdToSelect = null;
// First try to find the current Flex selected tree in the list
if(flexSelectedTreeID) {
treeIdToSelect = flexSelectedTreeID;
}
// Else try to find the current Flex focused tree in the list
else if(flexFocusedTreeID) {
treeIdToSelect = flexFocusedTreeID;
}
if(treeIdToSelect) {
var selectedTreeInList = BRO_inButtonController.getSelectedTree();
if(!selectedTreeInList || selectedTreeInList.treeID != treeIdToSelect) {
if(BRO_inButtonController.isTreeIdInTreeList(treeIdToSelect)) {
BRO_inButtonController.selectTree(treeIdToSelect);
}
// If the tree is not in the list, we refresh the list.
// (without alert if the user is not logged).
else {
BRO_inButtonController.selectTree(treeIdToSelect);
BRO_model.skipNextRequestValidation = true;
BRO_model.getTreesAndCurrentUser();
}
}
}
}
}
}else{
BRO_buttonEffectHelper.stopHelpEffects();
}
if(this.recordingMode == RECORING_MODE_ONE_BY_ONE) {
BRO_recordButtonController.refreshRecordButtonLabel(BRO_toolbar.isRecording);
}
},
getFlexSelectedUserIdFromAnchorParams:function() {
var params = this.getCurrentUrlAnchorParams();
var selectionParam = params['N-u'];
if(selectionParam && selectionParam.indexOf('_') != -1) {
return selectionParam.split("_")[1];
}
else {
return null;
}
},
getFlexSelectedTreeIdFromAnchorParams:function() {
var params = this.getCurrentUrlAnchorParams();
var selectionParam = params['N-s'];
if(selectionParam && selectionParam.indexOf('_') != -1) {
return selectionParam.split("_")[1];
}
else {
return null;
}
},
getFlexFocusedTreeIdFromAnchorParams:function() {
var params = this.getCurrentUrlAnchorParams();
var focusParam = params['N-f'];
if(focusParam && focusParam.indexOf('_') != -1) {
return focusParam.split("_")[1];
}
else {
return null;
}
},
getCurrentPearltreesPublicUrl:function() {
var currentUrl = BRO_browserManager.getSelectedBrowserUrl();
var pearltreesUrl = null;
// If the current tab display pearltrees
if(currentUrl && currentUrl.lastIndexOf(BRO_PUBLIC_URL) == 0) {
pearltreesUrl = currentUrl.substr(0,currentUrl.indexOf('#'));
}
if(!pearltreesUrl) {
pearltreesUrl = BRO_PUBLIC_URL+"/";
}
return pearltreesUrl;
},
getCurrentUrlAnchorParams:function() {
var currentUrl = BRO_browserManager.getSelectedBrowserUrl();
var params = [];
var paramsString = currentUrl.substr(currentUrl.indexOf('#')+1,currentUrl.length);
if(paramsString.length==0) {
return params;
}
else if(paramsString.indexOf('&') != -1) {
var nvPairs = paramsString.split("&");
for (i = 0; i < nvPairs.length; i++) {
var nvPair = nvPairs[i].split("=");
var name = nvPair[0];
var value = nvPair[1];
params[name] = value;
}
}
else {
var nvPair = paramsString.split("=");
var name = nvPair[0];
var value = nvPair[1];
params[name] = value;
}
return params;
},
isPearltreesPublicUrl:function() {
var currentUrl = BRO_browserManager.getSelectedBrowserUrl();
// If the current tab display pearltrees
return (currentUrl.lastIndexOf(BRO_PUBLIC_URL) == 0);
},
showOptions: function() {
var win = window.openDialog("chrome://broceliand/content/view/options.xul",
"PrefWindow",
"chrome=yes," +
"titlebar=yes," +
"toolbar=yes," +
"centerscreen=yes," +
"dialog=no");
BRO_toolbar.setOptionWindow(win);
},
setOptionWindow:function(value) {
BRO_toolbar.optionWindow = value;
},
getOptionWindow:function() {
return BRO_toolbar.optionWindow;
},
record: function(){
BRO_ButtonsHandler.startRecording();
},
showLoginForm: function() {
if(this.isRecording) {
BRO_toolbar.setRecording(false);
}
// This page handle the main pearltrees account stuffs.
BRO_tools.openAndReuseOneTabPerURL(BRO_SERVICE_FF_URL+"login");
},
isBrowserVersionGreaterOrEqual:function(versionToCompare) {
var appInfo = Components.classes["@mozilla.org/xre/app-info;1"]
.getService(Components.interfaces.nsIXULAppInfo);
var versionChecker = Components.classes["@mozilla.org/xpcom/version-comparator;1"]
.getService(Components.interfaces.nsIVersionComparator);
return (versionChecker.compare(appInfo.version, versionToCompare) >= 0);
},
isUserWantToLogin: function() {
var args = { wantLogin: 'false' };
var dialog = window.openDialog("chrome://broceliand/content/view/popup/loginPopup.xul",
"",
"chrome=yes," +
"dialog=yes," +
"modal=yes," +
"centerscreen=yes," +
"resizable=no",
args);
return args.wantLogin;
},
showHelpRecording:function() {
BRO_ButtonsHandler.closeButtonPopups();
var popupX = 50;
var popupY = 200;
var recordButton = document.getElementById('BRO_recordButton');
if(recordButton) {
popupX = recordButton.boxObject.screenX;
popupY = recordButton.boxObject.screenY + recordButton.boxObject.height;
}
// @see https://developer.mozilla.org/en/DOM/window.open#Window_functionality_features
var args = {isStart:true};
var win = window.openDialog("chrome://broceliand/content/view/popup/helpPopup.xul",
"helpStartup",
"all=no," +
"titlebar=no," +
"chrome=yes," +
"toolbar=no," +
"dialog=no," +
"resizable=no," +
"modal=yes," +
"dependent=yes," +
"top="+popupY+"px," +
"left="+popupX+"px," +
"width=421," +
"height=271",
args);
// width = image width + 39
// height = image height + 63
BRO_toolbar.helpStartupWindow = win;
},
showHelp:function() {
BRO_ButtonsHandler.closeButtonPopups();
var popupX = 50;
var popupY = 200;
var recordButton = document.getElementById('BRO_recordButton');
if(recordButton) {
popupX = recordButton.boxObject.screenX;
popupY = recordButton.boxObject.screenY + recordButton.boxObject.height;
}
// @see https://developer.mozilla.org/en/DOM/window.open#Window_functionality_features
var args = {isStart:false};
var win = window.openDialog("chrome://broceliand/content/view/popup/helpPopup.xul",
"help",
"all=no," +
"titlebar=no," +
"chrome=yes," +
"toolbar=no," +
"dialog=no," +
"resizable=no," +
"modal=yes," +
"dependent=yes," +
"top="+popupY+"px," +
"left="+popupX+"px," +
"width=345," +
"height=170",
args);
// width = image width + 39
// height = image height + 63
BRO_toolbar.helpStartupWindow = win;
},
showNotePopup:function() {
BRO_ButtonsHandler.closeButtonPopups();
var notePanel = document.getElementById('BRO_notePanel');
// Use Panels if possible (firefox 3.0+ only).
if(this.isBrowserVersionGreaterOrEqual("3.0") && notePanel) {
var recordButton = document.getElementById('BRO_recordButton');
notePanel.openPopup(recordButton, "after_start");
}
else{
var popupX = 50;
var popupY = 200;
var recordButton = document.getElementById('BRO_recordButton');
if(recordButton) {
popupX = recordButton.boxObject.screenX;
popupY = recordButton.boxObject.screenY + recordButton.boxObject.height;
}
// @see https://developer.mozilla.org/en/DOM/window.open#Window_functionality_features
var args = {inn:{defaultText: BRO_noteController.getDefaultText()}, out:null};
var win = window.openDialog("chrome://broceliand/content/view/popup/notePopup.xul",
"",
"all=no," +
"titlebar=no," +
"chrome=yes," +
"toolbar=no," +
"dialog=no," +
"resizable=no," +
"modal=yes," +
"dependent=yes," +
"top="+popupY+"px," +
"left="+popupX+"px",
args);
var params = args.out;
if (params && params.confirm) {
BRO_noteController.saveNoteAndRecordIfNotRecorded(params.noteText);
}
}
},
showNameMapWindow:function() {
BRO_ButtonsHandler.closeButtonPopups();
var newTreePanel = document.getElementById('BRO_newTreePanel');
// Use Panels if possible (firefox 3.0+ only).
if(this.isBrowserVersionGreaterOrEqual("3.0") && newTreePanel) {
var newButton = document.getElementById('BRO_newButton');
newTreePanel.openPopup(newButton, "after_start");
}
else {
if(BRO_toolbar.nameMapWindow && !BRO_toolbar.nameMapWindow.closed) {
return;
}
var popupX = 50;
var popupY = 200;
var newButton = document.getElementById('BRO_newButton');
if(newButton) {
popupX = newButton.boxObject.screenX;
popupY = newButton.boxObject.screenY + newButton.boxObject.height;
}
// @see https://developer.mozilla.org/en/Code_snippets/Dialogs_and_Prompts#Passing_arguments_and_displaying_a_dialog
// @see https://developer.mozilla.org/en/DOM/window.open#Window_functionality_features
var args = {inn:{confirm: 'false', mapName:null}, out:null};
var win = window.openDialog("chrome://broceliand/content/view/popup/nameMapPopup.xul",
BRO_locale.getString('popup.nameMap.title'),
"titlebar=no," +
"chrome=yes," +
"toolbar=no," +
"dialog=no," +
"resizable=no," +
"modal=yes," +
"dependent=yes," +
"top="+popupY+"px," +
"left="+popupX+"px",
args);
if(args.out) {
BRO_toolbar.nameMapWindow = win;
if (args.out && args.out.confirm) {
BRO_inButtonController.createNewTree(args.out.mapName);
} else {
BRO_inButtonController._selectedNewHistory = null;
}
}
}
},
showNameMapDoneWindow:function() {
BRO_ButtonsHandler.closeButtonPopups();
var popupX = 50;
var popupY = 400;
var newButton = document.getElementById('BRO_newButton');
if(newButton) {
popupX = newButton.boxObject.screenX;
popupY = newButton.boxObject.screenY + newButton.boxObject.height;
}
var args = {inn:{isCurrentUrlRecorded: this.isCurrentUrlRecorded()}, out:null};
var win = window.openDialog("chrome://broceliand/content/view/popup/nameMapDonePopup.xul",
"",
"titlebar=no," +
"chrome=yes," +
"toolbar=no," +
"dialog=no," +
"resizable=no," +
"modal=yes," +
"dependent=yes," +
"top="+popupY+"px," +
"left="+popupX+"px",
args);
},
showTreeFullWindow:function() {
BRO_ButtonsHandler.closeButtonPopups();
var popupX = 50;
var popupY = 200;
var recordButton = document.getElementById('BRO_recordButton');
if(recordButton) {
popupX = recordButton.boxObject.screenX;
popupY = recordButton.boxObject.screenY + recordButton.boxObject.height;
}
var args = { createNewMap: 'false', pearlCount: BRO_toolbar.getSelectionPearlCount() };
// @see https://developer.mozilla.org/en/DOM/window.open#Window_functionality_features
var win = window.openDialog("chrome://broceliand/content/view/popup/treeFullPopup.xul", "treeFull",
"titlebar=no," +
"chrome=yes," +
"toolbar=no," +
"dialog=no," +
"resizable=no," +
"modal=yes," +
"dependent=yes," +
"top="+popupY+"px," +
"left="+popupX+"px",
args);
if(args.createNewMap) {
BRO_inButtonController.showNameMapWindow(true);
}
else{
var selectedTree = BRO_inButtonController.getSelectedTree();
var rootTree = BRO_model.getRootTree();
if(selectedTree && selectedTree.treeID == rootTree.treeID && selectedTree.pearlCount >= MAX_PEARLS_IN_TREE) {
BRO_inButtonController.selectDropZoneTree();
}
}
},
showManyPearlsWindow:function() {
BRO_ButtonsHandler.closeButtonPopups();
var popupX = 50;
var popupY = 200;
var recordButton = document.getElementById('BRO_recordButton');
if(recordButton) {
popupX = recordButton.boxObject.screenX;
popupY = recordButton.boxObject.screenY + recordButton.boxObject.height;
}
var args = { createNewMap: 'false', pearlCount: BRO_toolbar.getSelectionPearlCount() };
// @see https://developer.mozilla.org/en/DOM/window.open#Window_functionality_features
var win = window.openDialog("chrome://broceliand/content/view/popup/manyPearlsPopup.xul", "manyPearls",
"titlebar=no," +
"chrome=yes," +
"toolbar=no," +
"dialog=no," +
"resizable=no," +
"modal=yes," +
"dependent=yes," +
"top="+popupY+"px," +
"left="+popupX+"px",
args);
BRO_toolbar.manyPearlsWindow = win;
if(args.createNewMap) {
BRO_inButtonController.showNameMapWindow();
}
},
showTreeDeletedMessage: function() {
// Stop recording effect
if(this.isRecording) {
BRO_toolbar.setRecording(false)
};
// Refresh tree list
BRO_inButtonController.selectRootTree();
BRO_model.getTreesAndCurrentUser();
// Show message
BRO_log.log("Can't continue this history. Tree has been deleted");
BRO_log.info(BRO_locale.getString('popup.error.treeDeleted'));
},
errorOnContinueHistory: function(errorCode) {
// Stop recording effect
if(this.isRecording) {
BRO_toolbar.setRecording(false)
};
// Refresh tree list
BRO_model.getTreesAndCurrentUser();
// Show message
BRO_log.log("Can't continue this history. Server error: "+errorCode);
},
onOnline:function() {
BRO_toolbar.isOnline = true;
var recordButton = document.getElementById("BRO_recordButton");
var newButton = document.getElementById("BRO_newButton");
if(recordButton) recordButton.disabled = false;
if(newButton) newButton.disabled = false;
},
onOffline:function() {
BRO_toolbar.isOnline = false;
var recordButton = document.getElementById("BRO_recordButton");
var newButton = document.getElementById("BRO_newButton");
if(recordButton) recordButton.disabled = true;
if(newButton) newButton.disabled = true;
},
/**
* Because our extension must be used in certain environements.
*/
validateEnv:function() {
return; // No more environment validation
// Check plateform
//BRO_log.log('Platform: ---------------------------');
var platform = navigator.platform;
var platformVersion = window.navigator.oscpu;
//BRO_log.log('OS: '+platform+' ('+platformVersion+') / Firefox:'+appInfo.version);
// Windows XP
if(platformVersion == 'Windows NT 5.1') {
var minVersion = "2";
if(versionChecker.compare(appInfo.version, minVersion) < 0) {
BRO_log.warning("Pearltrees plugin currently does not fully support your firefox version.\n"+
"On Windows XP we only support firefox "+minVersion+" "+
"and you are running firefox "+appInfo.version+"\n"+
"Contact the pearltrees team for more info.");
}
}
// Windows Vista
else if(platformVersion == 'Windows NT 6.0'){
var minVersion = "2";
if(versionChecker.compare(appInfo.version, minVersion) < 0) {
BRO_log.warning("Pearltrees plugin currently does not fully support your firefox version.\n"+
"On Windows Vista we only support firefox "+minVersion+" "+
"and you are running firefox "+appInfo.version+"\n"+
"Contact the pearltrees team for more info.");
}
}
// Mac
else if(platform == 'MacPPC' || platform == 'MacIntel') {
var minVersion = "3";
if(versionChecker.compare(appInfo.version, minVersion) < 0) {
BRO_log.warning("Pearltrees plugin currently does not fully support your firefox version.\n"+
"On Mac we only support firefox "+minVersion+" "+
"and you are running firefox "+appInfo.version+"\n"+
"Contact the pearltrees team for more info.");
}
}
// Linux
else if(platform == 'Linux i686' || platform == 'Linux x86_64' || platform == 'Linux i686 (x86_64)'){
var minVersion = "3";
if(versionChecker.compare(appInfo.version, minVersion) < 0) {
BRO_log.warning("Pearltrees plugin currently does not fully support your firefox version.\n"+
"On Linux we only support firefox "+minVersion+" "+
"and you are running firefox "+appInfo.version+"\n"+
"Contact the pearltrees team for more info.");
}
}
// Others
else {
BRO_log.error("Pearltrees plugin currently does not fully support your operating system: "+platform+".");
}
// Check plugins
//BRO_log.log('Plugins: ----------------------------');
var plugins = navigator.plugins;
for(var i=0; i<plugins.length; i++) {
var name = plugins[i].name;
//BRO_log.log(name);
}
//BRO_log.log(plugins.length+' plugins registered');
// Check extensions, we use here FUEL, created in firefox 3
//BRO_log.log('Extensions: ----------------------------');
if(typeof(Application) != 'undefined') {
var extensions = Application.extensions.all;
for(var i=0; i<extensions.length; i++) {
var ext = extensions[i];
if(!ext.enabled) continue;
}
}
},
// @see https://developer.mozilla.org/en/Code_snippets/Interaction_between_privileged_and_non-privileged_pages
handleFlexCommand: function(event) {
if(!this.isPearltreesPublicUrl()) {
return;
}
var commandName = event.target.getAttribute("commandName");
if(commandName == "login") {
if(this.isFirstInstallMode) {
this.setFirstInstallMode(false);
}
BRO_model.getTreesAndCurrentUser();
}
else if(commandName == "logout") {
BRO_model.resetModel();
}
else if(commandName == "detectPearlbar") {
var mainWindowDocument = content.document;
var element = mainWindowDocument.createElement("pearlbarCommandEvent");
element.setAttribute("commandName", "pearlbarIsInstalled");
mainWindowDocument.documentElement.appendChild(element);
var event = mainWindowDocument.createEvent("Events");
event.initEvent("pearlbarCommandEvent", true, false);
element.dispatchEvent(event);
}
},
getMainWindowDocument:function() {
return window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
.getInterface(Components.interfaces.nsIWebNavigation)
.QueryInterface(Components.interfaces.nsIDocShellTreeItem)
.rootTreeItem
.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
.getInterface(Components.interfaces.nsIDOMWindow)
.document;
},
initFlexCommandListener:function () {
var mainWindowDocument = this.getMainWindowDocument();
mainWindowDocument.addEventListener("flexCommandEvent", function(e) { BRO_toolbar.handleFlexCommand(e); }, false, true);
}
};
BRO_toolbar.init();